package com.zhou.juc.test09_Interview;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author zhouyanxiang
 * @create 2020-08-2020/8/15-12:38
 */
public class T02_WithVolatile {

    volatile List lists = Collections.synchronizedList(new ArrayList());

    public void add(Object o){
        lists.add(o);
    }

    public int size(){
        return lists.size();
    }

    public static void main(String[] args) {
        T02_WithVolatile t = new T02_WithVolatile();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                t.add(i);
                System.out.println("add " + i);
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t1").start();


        new Thread(()->{
            while(true){
                if (t.size() == 5){
                    break;
                }
            }
            System.out.println("======T2=======");
        },"t2").start();
    }

}
